package com.ruima.study.jdk21.jdk21.demo1;

/**
 * <p>
 * <p></p>
 * <p>
 * <p>Copyright (c) 2024/11/14 15:34, 科大讯飞 All Rights Reserve</p>
 *
 * @author ruima6
 * @version [V1.0]
 */
public class ScopeValueDemo3 {
        private static final ScopedValue<String> GIFT = ScopedValue.newInstance();

        public static void main(String[] args) {
            ScopeValueDemo3 t = new ScopeValueDemo3();
            t.giveGift();
        }

        public void giveGift() {
            ScopedValue.where(GIFT, "500元购物卡").run(() -> receiveMiddleMan());
        }


        //中间人
        public void receiveMiddleMan(){
            System.out.println(GIFT.get());//500
            //修改GIFT中的值，仅对run中调用的receiveGift方法生效
            ScopedValue.where(GIFT, "200元购物卡").run(() -> receiveGift());
            System.out.println(GIFT.get());//500
        }

        public void receiveGift() {
            System.out.println(GIFT.get()); //200
        }

    }

